package com.technology.client;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class IPUtil {
	/**
	 * 直接根据第一个网卡地址作为其内网ipv4地址  码云找JNDI-Injection-Exploit
	 *
	 * @return
	 */
	public static String getLocalIpByNetcard() {
		try {
			for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) {
				NetworkInterface item = e.nextElement();
				for (InterfaceAddress address : item.getInterfaceAddresses()) {
					if (item.isLoopback() || !item.isUp()) {
						continue;
					}
					if (address.getAddress() instanceof Inet4Address) {
						Inet4Address inet4Address = (Inet4Address) address.getAddress();
						return inet4Address.getHostAddress();
					}
				}
			}
			return InetAddress.getLocalHost().getHostAddress();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
